package com.czk.ssarag.config;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.springframework.ai.document.Document;
import org.springframework.ai.reader.TextReader;
import org.springframework.ai.transformer.splitter.TokenTextSplitter;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.nio.charset.Charset;
import java.util.List;

/**
 * @Author: chenzhangkun
 * @CreateTime: 2025-10-31
 * @Description:
 * @Version: 1.0
 */
@Configuration
public class InitVectorDatabaseConfig {

    @Resource(name = "simpleVectorStore")
    private SimpleVectorStore vectorStore;

    @Value(

            "classpath:czk.txt"
    )
    private org.springframework.core.io.Resource opsFile;

    @PostConstruct
    public void init() {
        // 读取文件
        TextReader textReader = new TextReader(opsFile);
        // 设置编码
        textReader.setCharset(Charset.defaultCharset());
        // 文件内容转化为向量，开启分词
        List<Document> list = new TokenTextSplitter().transform(textReader.read());
        // 写入向量数据库redisstack
        vectorStore.add(list);
    }

}
